<?php
/**
* @file $Id: ProgressReports.php 476 2007-04-27 05:12:08Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

include 'ProgramFunctions/_makeLetterGrade.fnc.php';

$course_period_id = UserCoursePeriod();
$course_id = DBGet(DBQuery("SELECT cp.COURSE_ID,c.TITLE FROM COURSE_PERIODS cp,COURSES c WHERE c.COURSE_ID=cp.COURSE_ID AND cp.COURSE_PERIOD_ID='".UserCoursePeriod()."'"));
$course_title = $course_id[1]['TITLE'];
$course_id = $course_id[1]['COURSE_ID'];

$config_RET = DBGet(DBQuery("SELECT TITLE,VALUE FROM PROGRAM_USER_CONFIG WHERE USERNAME='".User('USERNAME')."' AND PROGRAM='Gradebook'"),array(),array('TITLE'));
if(count($config_RET))
{
	foreach($config_RET as $title=>$value)
		$programconfig[$title] = $value[1]['VALUE'];
}

// ASSIGNMENT TYPES
$sql = "SELECT ASSIGNMENT_TYPE_ID,TITLE,FINAL_GRADE_PERCENT FROM GRADEBOOK_ASSIGNMENT_TYPES WHERE STAFF_ID='".User('STAFF_ID')."' AND COURSE_ID='".$course_id."' ORDER BY TITLE";
$types_RET = DBGet(DBQuery($sql));

$extra['force_search'] = true;
$extra['action'] .= "&_FOCUS_PDF=true";
Widgets('mailing_labels');

if(!$_REQUEST['search_modfunc'] || $_REQUEST['search_modfunc']=='search' || $_FOCUS['modules_search'])
{
	DrawHeader(_('Gradebook').' - '.ProgramTitle());

	$extra['new'] = true;
	Search('student_id',$extra);
}
else
{
	$RET = GetStuList($extra);
	
	if(count($RET))
	{
		$handle = PDFStart();
		foreach($RET as $student)
		{
			$sum_points = $nonweighted_total_points = $nonweighted_student_points = 0;
			$student_points = $total_points = array();
			unset($_FOCUS['DrawHeader']);

			if($_REQUEST['mailing_labels']=='Y')
				echo '<BR><BR><BR>';
			DrawHeader(Config('TITLE').' '._('Progress Report'));
			DrawHeader($student['FULL_NAME'],$student['STUDENT_ID']);
			DrawHeader($student['GRADE_ID'],GetSchool(UserSchool()));
			DrawHeader($course_title,GetMP(UserMP()));
			DrawHeader(ProperDate(DBDate()));

			if($_REQUEST['mailing_labels']=='Y')
				echo '<BR><BR><TABLE width=100%><TR><TD width=50> &nbsp; </TD><TD>'.$student['MAILING_LABEL'].'</TD></TR></TABLE><BR>';
			
			if(Preferences('WEIGHT','Gradebook')=='Y')
				$sql = "SELECT a.TITLE,a.ASSIGNED_DATE,t.ASSIGNMENT_TYPE_ID,a.ASSIGNMENT_TYPE_ID AS TYPE_ID,t.FINAL_GRADE_PERCENT,g.POINTS,".db_case(array('g.POINTS',"'-1'","'0'",'a.POINTS'))." AS TOTAL_POINTS,g.COMMENT,'' AS LETTER_GRADE FROM GRADEBOOK_ASSIGNMENT_TYPES t,GRADEBOOK_ASSIGNMENTS a LEFT OUTER JOIN GRADEBOOK_GRADES g ON (a.ASSIGNMENT_ID=g.ASSIGNMENT_ID AND g.STUDENT_ID='$student[STUDENT_ID]' AND g.COURSE_PERIOD_ID='".UserCoursePeriod()."') WHERE a.ASSIGNMENT_TYPE_ID=t.ASSIGNMENT_TYPE_ID AND ((a.COURSE_ID='$course_id' AND a.STAFF_ID='".User('STAFF_ID')."') OR a.COURSE_PERIOD_ID='".UserCoursePeriod()."') AND t.COURSE_ID='$course_id' AND a.MARKING_PERIOD_ID='".UserMP()."'";
			else
				$sql = "SELECT a.TITLE,a.ASSIGNED_DATE,'-1' AS ASSIGNMENT_TYPE_ID,a.ASSIGNMENT_TYPE_ID AS TYPE_ID,'1' AS FINAL_GRADE_PERCENT,g.POINTS,".db_case(array('g.POINTS',"'-1'","'0'",'a.POINTS'))." AS TOTAL_POINTS,g.COMMENT,'' AS LETTER_GRADE FROM GRADEBOOK_ASSIGNMENTS a LEFT OUTER JOIN GRADEBOOK_GRADES g ON (a.ASSIGNMENT_ID=g.ASSIGNMENT_ID AND g.STUDENT_ID='$student[STUDENT_ID]' AND g.COURSE_PERIOD_ID='".UserCoursePeriod()."') WHERE ((a.COURSE_ID='$course_id' AND a.STAFF_ID='".User('STAFF_ID')."') OR a.COURSE_PERIOD_ID='".UserCoursePeriod()."') AND a.MARKING_PERIOD_ID='".UserMP()."'";

			$grades_RET = DBGet(DBQuery($sql),array('ASSIGNED_DATE'=>'_removeSpaces','TITLE'=>'_removeSpaces','POINTS'=>'_makeExtra','LETTER_GRADE'=>'_makeExtra'));

			$columns = array('TITLE'=>_('Assignment'),'ASSIGNED_DATE'=>_('Assigned'),'POINTS'=>_('Points'),'LETTER_GRADE'=>_('Grade'),'COMMENT'=>_('Comment'));
			foreach($student_points as $assignment_type_id=>$points)
				$sum_points += $points / $total_points[$assignment_type_id];

			if(count($types_RET)==1)
			{
				$link['add']['html'] = array('TITLE'=>'<B>'._('Total').'</B>','LETTER_GRADE'=>Percent($sum_points).'&nbsp;'._makeLetterGrade($sum_points,UserCoursePeriod()));
				$link['add']['html']['ASSIGNED_DATE'] = $link['add']['html']['COMMENT'] = ' &nbsp; '; 
				$link['add']['html']['POINTS'] = $nonweighted_student_points.' / '.$nonweighted_total_points;
			}

			ListOutput($grades_RET,$columns,_('Assignment'),_('Assignments'),$link,array(),array('center'=>false,'add'=>true));

			if(count($types_RET)>1)
			{
				echo '<!-- NEED 8 -->';
				echo '<BR><b>'._('Totals:').'</b>';
				echo '<TABLE border=1 cellpadding=2>';
				echo '<TR><TH></TH>';
				foreach($types_RET as $type)
				{
					echo '<TH>'.$type['TITLE'].'</TH>';
				}
				echo '<TD rowspan=3> &nbsp;</TD>';
				echo '<TH>'._('Total Points').'</TH>';
				if(Preferences('WEIGHT','Gradebook')=='Y')
					echo '<TH>'._('Weighted Grade').'</TH>';
				echo '</TR>';
				if(Preferences('WEIGHT','Gradebook')=='Y')
				{
					echo '<TR><TH><b>'._('Percent of Grade').'</b></TH>';
					foreach($types_RET as $type)
					{
						echo '<TD>'.round($type['FINAL_GRADE_PERCENT']*100,0).'%</TD>';
					}
					echo '<TD>100%</TD>';
					echo '<TD>&nbsp;</TD>';
					echo '</TR>';
				}

				echo '<TR><TH><b>'._('Your Score').'</b></TH>';
				foreach($types_RET as $type)
				{
					if(Preferences('WEIGHT','Gradebook')!=='Y')
						$type['FINAL_GRADE_PERCENT'] = 1;
					$percent = $student_points[$type['ASSIGNMENT_TYPE_ID']] / $type['FINAL_GRADE_PERCENT'] / $total_points[$type['ASSIGNMENT_TYPE_ID']];
					echo '<TD>'.($student_points[$type['ASSIGNMENT_TYPE_ID']]/$type['FINAL_GRADE_PERCENT']).' / '.$total_points[$type['ASSIGNMENT_TYPE_ID']].' &nbsp;'.Percent($percent,0).' '._makeLetterGrade($percent,UserCoursePeriod()).'</TH>';
				}
				echo '<TD>'.$nonweighted_student_points.' / '.$nonweighted_total_points;
				if(Preferences('WEIGHT','Gradebook')!=='Y')
				{
					$percent = $nonweighted_student_points / $nonweighted_total_points;
					echo ' &nbsp;'.Percent($percent,0).' &nbsp;'._makeLetterGrade($percent,UserCoursePeriod());
				}
				else
					echo '</TD><TD>'.Percent($sum_points,0).' &nbsp;'._makeLetterGrade($sum_points,UserCoursePeriod());
				echo '</TD>';
				echo '</TR>';
				echo '</TABLE>';
			}			
			
			
			echo '<!-- NEW PAGE -->';
		}
		PDFStop($handle);
	}
	else
		BackPrompt(_('No Students were found.'));
}

function _makeExtra($value,$column)
{	global $THIS_RET,$student_points,$total_points,$nonweighted_student_points,$nonweighted_total_points,$_FOCUS;
	
	if($column=='POINTS')
	{
		if($value=='-1')
			return '<TABLE border=0 cellspacing=0 cellpadding=0 class=innerLO_field><TR><TD><font size=-1>'._('Excluded').'</font></TD><TD></TD><TD></TD></TR></TABLE>';
		else
		{
			if(Preferences('WEIGHT','Gradebook')!=='Y')
				$THIS_RET['FINAL_GRADE_PERCENT'] = 1;
			$student_points[$THIS_RET['TYPE_ID']] += $value * $THIS_RET['FINAL_GRADE_PERCENT'];
			$total_points[$THIS_RET['TYPE_ID']] += $THIS_RET['TOTAL_POINTS'];
	
			$nonweighted_student_points += $value;
			$nonweighted_total_points += $THIS_RET['TOTAL_POINTS'];

			return '<TABLE border=0 cellspacing=0 cellpadding=0 class=innerLO_field><TR><TD>'.($THIS_RET['POINTS']*1).'</TD><TD>&nbsp;/&nbsp;</TD><TD>'.$THIS_RET['TOTAL_POINTS'].'</TD></TR></TABLE>';
		}
	}
	elseif($column=='LETTER_GRADE')
	{
		if($THIS_RET['POINTS']=='-1')
			return _('N/A').'&nbsp;'._('N/A');
		elseif($THIS_RET['TOTAL_POINTS']==0)
			return _('EC');
		else
			return Percent($THIS_RET['POINTS']/$THIS_RET['TOTAL_POINTS'],0) .'&nbsp;'. _makeLetterGrade($THIS_RET['POINTS']/$THIS_RET['TOTAL_POINTS'],UserCoursePeriod());
	}
}

function _removeSpaces($value,$column)
{
	if($column=='ASSIGNED_DATE')
		$value = ProperDate($value);
	
	return str_replace(' ','&nbsp;',str_replace('&','&amp;',$value));
}

?>